﻿using Glorysoft.EAP.EAPClient.ViewModel;
using System.Runtime.Remoting;
using System.Windows;

namespace Glorysoft.EAP.EAPClient.Common
{
    public class Controller
    {
        /// <summary>
        /// show window
        /// </summary>
        /// <typeparam name="T">ViewModel Type</typeparam>
        /// <param name="vm">ViewModel instance</param>
        public static void ShowSubWindow<T>(T vm) where T :PopupWindowVM
        {
            var viewType = Consts.ViewModelViewMap[typeof(T).Name];
            ObjectHandle handler = System.Activator.CreateInstance(null, Consts.ViewNameSpace + "." + viewType);
            var view = (Window)handler.Unwrap();
            view.DataContext = vm;
            vm.CloseParentWindow = () => view.Close();
            vm.CloseWindowEvent = () => view.Close();
            view.ShowDialog();
        }
        public static void ShowWindow<T>(T vm) where T : PopupWindowVM
        {
            var viewType = Consts.ViewModelViewMap[typeof(T).Name];
            ObjectHandle handler = System.Activator.CreateInstance(null, Consts.ViewNameSpace + "." + viewType);
            var view = (Window)handler.Unwrap();
            view.DataContext = vm;
            vm.CloseParentWindow = () => view.Close();
            vm.CloseWindowEvent = () => view.Close();
            view.Show();
        }
    }
}
